<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col v-if="this.$route.query.source === 'BF_PORTFOLIO'" :span="24">
          <el-form-item :label="$t('母基金名称')" prop="dealId">
            <el-select v-model="form.dealId" :placeholder="$t('母基金名称')" filterable :disabled="form.id">
              <el-option
                v-for="item in bfBaseList"
                :key="item.id"
                :value="item.dealId"
                :label="item.dealName"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <el-input v-model="form.fundName" :placeholder="$t('基金名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金管理人名称')" prop="managerName">
            <el-input v-model="form.managerName" :placeholder="$t('基金管理人名称')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金注册成立日')" prop="fundSetTime">
            <el-date-picker
              v-model="form.fundSetTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('基金注册成立日')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金期限')" prop="fundTerm">
            <el-input v-model="form.fundTerm" :placeholder="$t('基金期限')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金当前阶段')" prop="fundPhase">
            <dictSelect
              v-model="form.fundPhase"
              :clearable="true"
              dictionary="fund_phase"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('首次出资时间')" prop="firstInvestmentTime">
            <el-date-picker
              v-model="form.firstInvestmentTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('首次出资时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金总规模')+$t('金额单位')" prop="fundSize">
            <Money v-model="form.fundSize" unit="w" :placeholder="$t('基金总规模')" @change="subscriptionRatio" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('母基金认缴金额')+$t('金额单位')" prop="subscriptionAmount">
            <Money v-model="form.subscriptionAmount" unit="w" :placeholder="$t('母基金认缴金额')" @change="subscriptionRatio" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('母基金认缴比例(%)')" prop="subscriptionRatio">
            <Money v-model="form.subscriptionRatio" :placeholder="$t('母基金认缴比例(%)')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('母基金实缴金额')+$t('金额单位')" prop="paidAmount">
            <Money v-model="form.paidAmount" unit="w" :placeholder="$t('母基金实缴金额')" @change="contributionProgress" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('母基金出资进度(%)')" prop="contributionProgress">
            <Money v-model="form.contributionProgress" :placeholder="$t('母基金出资进度(%)')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('母基金回收金额')+$t('金额单位')" prop="recoveryAmount">
            <Money v-model="form.recoveryAmount" unit="w" :placeholder="$t('母基金回收金额')+$t('金额单位')" @change="recoveryProgress" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('母基金回收进度(%)')" prop="recoveryProgress">
            <Money v-model="form.recoveryProgress" :placeholder="$t('母基金回收进度(%)')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('母基金持有最新估值')+$t('金额单位')" prop="valuation">
            <Money v-model="form.valuation" unit="w" :placeholder="$t('母基金持有最新估值')+$t('金额单位')" @change="returnMultiple" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回报倍数')" prop="returnMultiple">
            <Money v-model="form.returnMultiple" :placeholder="$t('回报倍数')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目状态')" prop="status">
            <dictSelect
              v-model="form.status"
              :clearable="true"
              dictionary="quit_status"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('基金简介')" prop="fundProfile">
            <el-input v-model="form.fundProfile" type="textarea" :placeholder="$t('基金简介')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="bf:investGroup" />
    </div>
  </div>
</template>

<script>
    import { saveBfInvestFund, getBfInvestFund } from '@/api/bf/investGroup/bfInvestFund'
    import { findAll } from '@/api/bf/base/bfBase'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  fundName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  managerName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  dealId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                // 参股基金列表
                bfBaseList: []
            }
        },
        created() {
          findAll().then(res => {
            this.bfBaseList = res.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getBfInvestFund(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    fundName: undefined,
                    managerName: undefined,
                    fundSetTime: undefined,
                    fundTerm: undefined,
                    fundPhase: undefined,
                    firstInvestmentTime: undefined,
                    fundSize: undefined,
                    subscriptionAmount: undefined,
                    subscriptionRatio: undefined,
                    paidAmount: undefined,
                    contributionProgress: undefined,
                    recoveryAmount: undefined,
                    recoveryProgress: undefined,
                    valuation: undefined,
                    returnMultiple: undefined,
                    status: undefined,
                    fundProfile: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                // 关闭form页面
                if (this.$parent.$parent.investFundOpen !== undefined) {
                  this.$parent.$parent.investFundOpen = false
                }
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 关闭form页面
                if (this.$parent.$parent.investFundOpen !== undefined) {
                  this.$parent.$parent.investFundOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveBfInvestFund(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 计算母基金认缴比例
          subscriptionRatio() {
            if (this.form.fundSize !== undefined && this.form.subscriptionAmount !== undefined && this.form.fundSize !== '' && this.form.subscriptionAmount !== '') {
              var fundSize = this.form.fundSize ? Number(this.form.fundSize) : 0
              var subscriptionAmount = this.form.subscriptionAmount ? Number(this.form.subscriptionAmount) : 0
              this.form.subscriptionRatio = subscriptionAmount / fundSize * 100
              this.contributionProgress()
            } else {
              this.form.subscriptionRatio = undefined
            }
          },
          // 计算母基金出资进度
          contributionProgress() {
            if (this.form.paidAmount !== undefined && this.form.subscriptionAmount !== undefined && this.form.paidAmount !== '' && this.form.subscriptionAmount !== '') {
              var paidAmount = this.form.paidAmount ? Number(this.form.paidAmount) : 0
              var subscriptionAmount = this.form.subscriptionAmount ? Number(this.form.subscriptionAmount) : 0
              this.form.contributionProgress = paidAmount / subscriptionAmount * 100
              this.recoveryProgress()
            } else {
              this.form.contributionProgress = undefined
            }
          },
          // 计算母基金回收进度
          recoveryProgress() {
            if (this.form.paidAmount !== undefined && this.form.recoveryAmount !== undefined && this.form.paidAmount !== '' && this.form.recoveryAmount !== '') {
              var paidAmount = this.form.paidAmount ? Number(this.form.paidAmount) : 0
              var recoveryAmount = this.form.recoveryAmount ? Number(this.form.recoveryAmount) : 0
              this.form.recoveryProgress = recoveryAmount / paidAmount * 100
              this.returnMultiple()
            } else {
              this.form.recoveryProgress = undefined
            }
          },
          // 计算回报倍数
          returnMultiple() {
            if (this.form.valuation !== undefined && this.form.valuation !== '') {
              var paidAmount = this.form.paidAmount ? Number(this.form.paidAmount) : 0
              var recoveryAmount = this.form.recoveryAmount ? Number(this.form.recoveryAmount) : 0
              var valuation = this.form.valuation ? Number(this.form.valuation) : 0
              this.form.returnMultiple = (recoveryAmount + valuation) / paidAmount
            } else {
              this.form.returnMultiple = undefined
            }
          }
        }
    }
</script>
